package cn.easy.mortgage.web;

import cn.easy.base.bean.PageBean;
import cn.easy.base.bean.common.AjaxResultBean;
import cn.easy.base.web.BaseController;
import cn.easy.mortgage.domain.EstateEvalInfo;
import cn.easy.mortgage.service.EstateEvalInfoService;
import cn.easy.mortgage.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author huangm
 */
@Controller
@RequestMapping("/estateEvalInfo")
public class EstateEvalInfoController extends BaseController {
    @Autowired
    private EstateEvalInfoService	estateEvalInfoService;

    @RequestMapping("")
    String index(Model model, HttpServletRequest request) {
        return "estateEvalInfo/index";
    }

    @RequestMapping("/list")
    @ResponseBody
    Page<EstateEvalInfo> list(PageBean pageBean, HttpServletRequest request) {
        Page<EstateEvalInfo> page = estateEvalInfoService.search(searchParams(request), pageBean);
        setConfigFieldValues(page, Constants.PROPERTY_TYPE_KEY, "propertyType");
        return page;
    }

    @RequestMapping(value = "/form", method = RequestMethod.GET)
    String formGet(@RequestParam(value = "id", required = false) String id, Model model, HttpServletRequest request) {
        if (isValidId(id)) {
            EstateEvalInfo estateEvalInfo = estateEvalInfoService.getOne(id);
            model.addAttribute("estateEvalInfo", estateEvalInfo);
        } else {
            model.addAttribute("estateEvalInfo", new EstateEvalInfo());
        }
        return "estateEvalInfo/form";
    }

    @RequestMapping(value = "/form", method = RequestMethod.POST)
    @ResponseBody
    AjaxResultBean formPost(EstateEvalInfo estateEvalInfo, HttpServletRequest request) {
        estateEvalInfoService.save(estateEvalInfo);
        return toSuccess("保存成功");
    }

    @RequestMapping("/delete")
    @ResponseBody
    AjaxResultBean delete(@RequestParam(value = "id") String id) {
        estateEvalInfoService.delete(id);
        return toSuccess("删除成功");
    }

    @RequestMapping("/view")
    String view(@RequestParam(value = "id") String id, Model model) {
        EstateEvalInfo estateEvalInfo = estateEvalInfoService.getOne(id);
        String propertyType = getConfigValue(Constants.PROPERTY_TYPE_KEY, estateEvalInfo.getPropertyType());
        model.addAttribute("propertyType", propertyType);
        model.addAttribute("estateEvalInfo", estateEvalInfo);
        return "estateEvalInfo/view";
    }

}


